import router from '@/router'
import { getSportsCode } from './sports'
import { SportsType } from '@/enums'
import { openUrl, getURLParam, goHelp, getQuery } from '@/utils'
import {
	useLayoutStore,
	useWalletStore,
	useAccountStore,
	useProfileStore,
	useFollowStore,
	useActivityStore
} from '@/store'
import { todo, customerService } from './app'
import { UpSwitchTabsEnum } from '@/components/up'
import { FollowShowType } from '@/types'

export const goHome = () => router.push({ name: 'home' })

export const goSports = () => router.push({ name: 'sports' })

export const goSlots = () => router.push({ name: 'slots' })

export const goCasino = () => router.push({ name: 'casino' })

export const goTournament = () => todo()

export const goWallet = () => {
	const walletStore = useWalletStore()
	walletStore.validateNavigation()
}

export const goLogin = () => {
	const accountStore = useAccountStore()
	!accountStore.isLogin && accountStore.showLogin()
}

export const goRegister = () => {
	const accountStore = useAccountStore()
	!accountStore.isLogin && accountStore.showRegister()
}

export const goThirdPartyCheck = () => {
	const accountStore = useAccountStore()
	accountStore.thirdPartyCheck().then()
}

export const goNotification = (val: string) => {
	let tab = 'notification'
	let id = 0
	if (val.startsWith('/inbox/announcement')) {
		tab = 'announcement'
		id = parseInt(val.replace('/inbox/announcement/', ''))
	}
	if (val.startsWith('/inbox/me')) {
		tab = 'me'
		id = parseInt(val.replace('/inbox/me/', ''))
	}
	if (val.startsWith('/inbox/notification')) {
		tab = 'notification'
		id = parseInt(val.replace('/inbox/notification/', ''))
	}
	const profileStore = useProfileStore()
	profileStore.showNotice({
		show: true,
		tab: tab,
		id: id ? id : undefined
	})
}

export const goFollow = (val: string) => {
	const userId = getURLParam(val, 'id')
	const type = getURLParam(val, 'type')
	let tab = FollowShowType.following
	if (type === 'request') {
		tab = FollowShowType.request
	}
	if (type === 'follower') {
		tab = FollowShowType.follow
	}
	const followStore = useFollowStore()
	if (userId) {
		followStore.showFollow({
			type: tab,
			show: true,
			userId: parseInt(userId),
			userName: '',
			nickName: ''
		})
	} else {
		const profileStore = useProfileStore()
		const accountStore = useAccountStore()
		if (accountStore.isLogin) {
			followStore.showFollow({
				type: tab,
				show: true,
				userId: profileStore.userInfo?.userId,
				userName: profileStore.userInfo?.userName,
				nickName: profileStore.userInfo?.nickName
			})
		}
	}
}

export const goAccount = () => {
	const profileStore = useProfileStore()
	profileStore.showProfileAccount(true)
}

export const goAbout = () => {
	goHelp()
}

export const goRewards = (val: string) => {
	const activityStore = useActivityStore()
	activityStore.changeTab(val)
	const layoutStore = useLayoutStore()
	layoutStore.setSidebar('activity')
}

export const goStatement = () => {
	const profileStore = useProfileStore()
	profileStore.setReportShow(true)
}

export const goSportsLeague = (sportId: SportsType, leagueId: string) => {
	const code = getSportsCode(sportId)
	router.push('/sports/' + code + '/' + leagueId)
}

export const goDetails = async (url: string | undefined, query = {}, options = {}) => {
	console.log(url, options)
	const route = router.currentRoute.value
	if (route.path.startsWith('/details/')) {
		// if (route.path.startsWith('/details/') || options.replace) {
		await router.replace({ path: `/details/${url}`, query })
		// window.location.reload()
	} else {
		await router.push({ path: `/details/${url}`, query })
	}
}

const removeLang = (path: string) => {
	// 如果url包含语言层级，就移除
	return path.replace(/^\/(zh|en|zhTW)/, '')
}
const popupList = [
	'/login',
	'/register',
	'/social-callback',
	'/wallet',
	'/inbox',
	'/profile',
	'/setting',
	'/account',
	'/rewards-home',
	'/statement',
	'/customer-service',
	'/follow',
	'/reset-password'
]

export const isPopupUrl = (fullPath: string) => {
	const url = removeLang(fullPath)
	return popupList.some((path) => url.startsWith(path))
}

export const popupUrl = (fullPath: string) => {
	const accountStore = useAccountStore()
	const profileStore = useProfileStore()
	const url = removeLang(fullPath)
	if (url.startsWith('/login')) {
		goLogin()
	} else if (url.startsWith('/register')) {
		goRegister()
	} else if (url.startsWith('/social-callback')) {
		goThirdPartyCheck()
	} else if (url.startsWith('/wallet')) {
		if (accountStore.isLogin) {
			goWallet()
		}
	} else if (url.startsWith('/profile')) {
		const userId = getURLParam(url, 'id') || profileStore.userInfo?.userId
		if (userId) {
			let tab = UpSwitchTabsEnum.sports
			if (url.startsWith('/profile/sports-stats')) {
				tab = UpSwitchTabsEnum.sports
			} else if (url.startsWith('/profile/slots-stats')) {
				tab = UpSwitchTabsEnum.slots
			} else if (url.startsWith('/profile/casino-stats')) {
				tab = UpSwitchTabsEnum.casino
			}
			profileStore.showProfile({
				show: true,
				userId,
				tab: tab
			})
		}
	} else if (url.startsWith('/inbox')) {
		if (accountStore.isLogin) {
			goNotification(url)
		}
	} else if (url === '/account') {
		if (accountStore.isLogin) {
			goAccount()
		}
	} else if (url.startsWith('/rewards-home')) {
		if (accountStore.isLogin) {
			if (url.indexOf('star') > 0) {
				goRewards('star')
			} else {
				goRewards('vip')
			}
		}
	} else if (url.startsWith('/statement')) {
		if (accountStore.isLogin) {
			goStatement()
		}
	} else if (url.startsWith('/customer-service')) {
		customerService()
	} else if (url.startsWith('/follow')) {
		goFollow(url)
	} else if (url.startsWith('/reset-password')) {
		const token = getQuery('token')
		token && accountStore.showReset(token)
	}
}

export const jumpRouteUrl = (url: string | undefined) => {
	if (!url) return false
	const httpReg = /(http|https):\/\/([\w.]+\/?)\S*/gi
	if (httpReg.test(url)) {
		openUrl(url)
	} else if (isPopupUrl(url)) {
		popupUrl(url)
	} else {
		router.push(url)
	}
}
